package me.leiusual.sharing.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import dagger.android.support.DaggerFragment
import kotlinx.android.synthetic.main.fragment_mine_invited.*
import me.leiusual.sharing.R
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.helper.HandleObserver
import me.leiusual.sharing.ui.mine.adapter.MineInvitedAdapter
import timber.log.Timber
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class MineInvitedFragment : DaggerFragment(), View.OnClickListener {

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val viewModel: MineInvitedViewModel by viewModels { factory }

  private val args: MineInvitedFragmentArgs by navArgs()

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_mine_invited, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    toolbar.setNavigationOnClickListener {
      findNavController().popBackStack()
    }

    button_renew.setOnClickListener(this)

    recycler_view.adapter = MineInvitedAdapter({
      Timber.v("点击：%s", it)

      val navDirections = MineInvitedFragmentDirections.actionMineInvitedFragmentSelf(args.targetUserId, it.id)
      findNavController().navigate(navDirections)

    }, {
      Timber.v("续费：%s", it)

      val navDirections = MineInvitedFragmentDirections.actionMineInvitedFragmentToMineRenewFragment(it.id)
      findNavController().navigate(navDirections)
    })

    observeData()
    viewModel.invited(args.currentUserId, args.targetUserId)
  }

  private fun observeData() {
    viewModel.loadInitial.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("loadInitial = %s", it)
      when (it) {
        is Data.Loading -> {
          state_constraint_layout.showProgress()
        }
        is Data.Error -> {
          state_constraint_layout.showError(it.error.message)
        }
        is Data.Success -> {
          state_constraint_layout.showContent()

          text_view_title.text = "${it.data.username}邀请的好友"
          text_view_invited_count.text = "邀请好友 ${it.data.invitedCount}人  ${it.data.storeInfo}"
        }
      }
    })

    viewModel.pagedList.observe(viewLifecycleOwner, Observer {
      Timber.v("pagedList = %s", it)

      if (it.isEmpty()) {
        state_constraint_layout.showEmpty()
      } else {
        state_constraint_layout.showContent()

        val adapter = recycler_view.adapter as MineInvitedAdapter
        adapter.submitList(it)
      }
    })
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.button_renew -> {
        val data = viewModel.loadInitial.value as? Data.Success
        data?.data?.id?.apply {
          val navDirections = MineInvitedFragmentDirections.actionMineInvitedFragmentToMineRenewFragment(this)
          findNavController().navigate(navDirections)
        }
      }
    }
  }
}